var spiralMatrixIII = function(R, C, r0, c0) {
    let count = 1
    let sum = R * C
    R--
    C--
    let lenRotate = 3
    let rsl = [
        [r0, c0],
    ]
    let start = [r0, c0]
    while (true) {
        if (count === sum) {
            return rsl
        }
        start = [start[0], start[1] + 1]
        for (let i = 0; i < lenRotate - 1; i++) {
            if (i !== lenRotate - 2) {
                if (start[0] + i <= R && start[1] <= C && start[0] + i >= 0 && start[1] >= 0) {
                    rsl.push([start[0] + i, start[1]])
                    if (++count === sum) {
                        return rsl
                    }
                }
            } else {
                start = [start[0] + i, start[1]]
            }
        }
        for (let i = 0; i < lenRotate; i++) {
            if (i !== lenRotate - 1) {
                if (start[0] <= R && start[1] - i <= C && start[0] >= 0 && start[1] - i >= 0) {
                    rsl.push([start[0], start[1] - i])
                    if (++count === sum) {
                        return rsl
                    }
                }
            } else {
                start = [start[0], start[1] - i]
            }
        }
        for (let i = 0; i < lenRotate; i++) {
            if (i !== lenRotate - 1) {
                if (start[0] - i <= R && start[1] <= C && start[0] - i >= 0 && start[1] >= 0) {
                    rsl.push([start[0] - i, start[1]])
                    if (++count === sum) {
                        return rsl
                    }
                }
            } else {
                start = [start[0] - i, start[1]]
            }
        }
        for (let i = 0; i < lenRotate; i++) {
            if (start[0] <= R && start[1] + i <= C && start[0] >= 0 && start[1] + i >= 0) {
                rsl.push([start[0], start[1] + i])
                if (++count === sum) {
                    return rsl
                }
            }
            if (i === lenRotate - 1) {
                start = [start[0], start[1] + i]
            }
        }
        lenRotate += 2
    }
};
console.log(spiralMatrixIII(1, 1, 0, 0))